import sys
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

#the module httplib2 was downloaded from:
#http://code.google.com/p/httplib2/downloads/list
#the module gflags was downloaded from:
#http://code.google.com/p/python-gflags/downloads/list

#This script is based on example here:
#http://code.google.com/p/google-api-python-client/wiki/GettingStarted

FLOW = OAuth2WebServerFlow(
    client_id='433807057907.apps.googleusercontent.com',
    client_secret='jigtZpMApkRxncxikFpR+SFg',
	scope='https://www.googleapis.com/auth/calendar',
	user_agent='moderator-cmdline-sample/1.0')

#Expect commandline arg of calendar id
def main(argv):
	storage = Storage('calendar.dat')
	credentials = storage.get()
	if credentials is None or credentials.invalid == True:
		credentials = run(FLOW, storage)

	http = httplib2.Http(cache=".cache")
	http = credentials.authorize(http)
	service = build("calendar","v3",http=http)
	res = get_all_events(service, argv[1])
	print res

def get_all_events(service, calId):
	#the function calls on service obj are taken from the api explorer
	#except there are extra parentheses after "events" to signify function call
	return service.events().list(calendarId=calId).execute()

if __name__ == '__main__':
	main(sys.argv)
